import 'package:dio/dio.dart';
import 'package:logger/logger.dart';

import '../../../data/dio/api/bt_dio_network.dart';
import '../../../data/dio/api/bt_net_data_source.dart';
import '../../../data/models/bt_goods.dart';

class BtDiscoverRepository {
  final logger = Logger();
  final BtNetDataSource network = BtDioNetwork();

  void fetchBtGoodsList(Function(List<BtGoods> btGoodsList) onResult) {
    var map = <String, dynamic>{};
    map['isIncludeSubscription'] = false;
    map['payChannel'] = 'IAP';
    network.fetchCoinGoodsList(map).then((result) {
      if (result.isSuccess) {
        onResult(result.data ?? []);
      }
    }).catchError((error) {
      // non-200 error goes here.
      switch (error.runtimeType) {
        case DioException:
          // Here's the sample to get the failed response error code and message
          final res = (error as DioException).response;
          logger.e('Got error : ${res?.statusCode} -> ${res?.statusMessage}');
          break;
        default:
          logger.e('Error occurred: $error');
          break;
      }
    });
  }

  void reviewModeConsume(int outlay, Function(bool, String) onResult) {
    var map = <String, dynamic>{};
    map['outlay'] = outlay;
    map['source'] = 'iap';
    network.reviewModeConsume(map).then((result) {
      onResult(result.isSuccess, result.isSuccess ? 'Success' : result.msg);
    }).catchError((error) {
      // non-200 error goes here.
      switch (error.runtimeType) {
        case DioException:
          // Here's the sample to get the failed response error code and message
          final res = (error as DioException).response;
          logger.e('Got error : ${res?.statusCode} -> ${res?.statusMessage}');
          break;
        default:
          logger.e('Error occurred: $error');
          break;
      }
    });
  }
}
